package com.woniu.tms.interview.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.woniu.tms.entity.Classes;
import com.woniu.tms.entity.ClassesExample;
import com.woniu.tms.entity.Companyinfo;
import com.woniu.tms.entity.CompanyinfoExample;
import com.woniu.tms.entity.Interview;
import com.woniu.tms.entity.InterviewExample;
import com.woniu.tms.entity.CompanyinfoExample.Criteria;
import com.woniu.tms.entity.User;
import com.woniu.tms.entity.UserExample;
import com.woniu.tms.entity.UserRole;
import com.woniu.tms.entity.UserRoleExample;
import com.woniu.tms.interview.service.CompanyService;
import com.woniu.tms.mapper.ClassesMapper;
import com.woniu.tms.mapper.CompanyinfoMapper;
import com.woniu.tms.mapper.InterviewMapper;
import com.woniu.tms.mapper.UserMapper;
import com.woniu.tms.mapper.UserRoleMapper;

@Service
@Transactional
public class CompanyServiceImpl implements CompanyService {
	@Autowired
	CompanyinfoMapper companyinfoMapper;
	@Autowired
	UserMapper userMapper;
	@Autowired
	UserRoleMapper userroleMapper;
	@Autowired
	InterviewMapper interviewMapper;
	@Autowired
	ClassesMapper classesMapper;

	@Override
	public List<User> findunwork() {
		UserExample example = new UserExample();
		com.woniu.tms.entity.UserExample.Criteria criteria = example.createCriteria();
		criteria.andUstatusEqualTo("未就业");
		List<User> list = userMapper.selectByExample(example);
		return list;
	}

	@Override
	public void updateAll(Integer[] uid) {
		for (Integer id : uid) {
			userMapper.updateByUid(id);
		}
	}

	@Override
	public void updateOne(Integer uid) {
		userMapper.updateByUid(uid);

	}

	@Override
	public String sendMail(String user, String password, String host, String from, String to, String subject,
			String content) {
		if (to != null) {
			Properties props = System.getProperties();
			props.put("mail.smtp.host", host);
			props.put("mail.smtp.auth", "true");
			Authenticator authenticator = new Authenticator() {
				@Override
				public PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication(user, password);
				}
			};
			Session session = Session.getInstance(props, authenticator);
			session.setDebug(true);
			try {
				MimeMessage message = new MimeMessage(session);
				message.setFrom(new InternetAddress(from));
				if (!to.trim().equals("")) {
					message.addRecipient(Message.RecipientType.TO, new InternetAddress(to.trim()));
				}
				message.setSubject(subject);
				MimeBodyPart mbp1 = new MimeBodyPart(); // 正文
				mbp1.setContent(content, "text/html;charset=utf-8");
				Multipart mp = new MimeMultipart(); // 整个邮件：正文+附件
				mp.addBodyPart(mbp1);
				// mp.addBodyPart(mbp2);
				message.setContent(mp);
				message.setSentDate(new Date());
				message.saveChanges();
				Transport.send(message);

			} catch (Exception e) {
				e.printStackTrace();
				return "failure";
			}
			return "success";
		} else {
			return "failure";
		}
	}

	@Override
	public User findOne(Integer id) {
		User user = userMapper.selectByPrimaryKey(id);
		return user;
	}

	@Override
	public List<User> finByUnameLike(String info) {
		UserExample example = new UserExample();
		com.woniu.tms.entity.UserExample.Criteria criteria = example.createCriteria();
		criteria.andUstatusEqualTo("未就业");
		criteria.andUnameLike("%" + info + "%");
		criteria.andSnoLike("%" + info + "%");
		example.or(criteria);
		List<User> list = userMapper.selectByExample(example);
		return list;
	}

	@Override
	public void add(User user, User loginUser) {
		CompanyinfoExample example = new CompanyinfoExample();
		Criteria criteria = example.createCriteria();
		criteria.andCompanyidEqualTo(loginUser.getUid());
		List<Companyinfo> list = companyinfoMapper.selectByExample(example);
		Companyinfo companyinfo = list.get(0);

		Interview interview = new Interview();
		interview.setStudentid(user.getUid());
		interview.setCompanyid(loginUser.getUid());
		interview.setEmpid(companyinfo.getEmpid());
		interviewMapper.insertSelective(interview);

	}

	@Override
	public List<Interview> findByUnameLike(String info, User loginUser) {

		List<Interview> list = new ArrayList<>();
		InterviewExample example = new InterviewExample();
		com.woniu.tms.entity.InterviewExample.Criteria criteria = example.createCriteria();
		criteria.andStudentidEqualTo(loginUser.getUid());
		List<Interview> list2 = interviewMapper.selectByExample(example);

		if (info != null && !info.trim().equals("")) {
			for (Interview interview : list2) {
				if (interview.getCompanyUser().getUname().contains(info)
						&& interview.getStudentid() == loginUser.getUid()) {
					list.add(interview);
				}
			}

			return list;
		}
		return list2;

	}

	@Override
	public List<Interview> findLikeUname(String info, User loginUser) {
		List<Interview> list = new ArrayList<>();
		List<Integer> list4 = new ArrayList<>();
		List<Integer> list8 = new ArrayList<>();
		UserRoleExample example5 = new UserRoleExample();
		com.woniu.tms.entity.UserRoleExample.Criteria criteria5 = example5.createCriteria();
		criteria5.andUidEqualTo(loginUser.getUid());
		List<UserRole> list9 = userroleMapper.selectByExample(example5);
		UserRole userRole = list9.get(0);
		System.out.println(userRole.getRid());
		if (userRole.getRid() == 2) {
			ClassesExample example4 = new ClassesExample();
			com.woniu.tms.entity.ClassesExample.Criteria criteria4 = example4.createCriteria();
			criteria4.andClassmanageridEqualTo(loginUser.getUid());
			List<Classes> list7 = classesMapper.selectByExample(example4);
			for (Classes classes : list7) {
				list8.add(classes.getCid());
			}

		} else if (userRole.getRid() == 3) {
			ClassesExample example6 = new ClassesExample();
			com.woniu.tms.entity.ClassesExample.Criteria criteria4 = example6.createCriteria();
			criteria4.andTeacheridEqualTo(loginUser.getUid());
			List<Classes> list7 = classesMapper.selectByExample(example6);
			System.out.println(list7.size());
			for (Classes classes : list7) {
				list8.add(classes.getCid());
			}

		}
		System.out.println(list8.size());
		ClassesExample example2 = new ClassesExample();
		com.woniu.tms.entity.ClassesExample.Criteria criteria2 = example2.createCriteria();
		criteria2.andCidIn(list8);
		List<Classes> list3 = classesMapper.selectByExample(example2);
		for (Classes classes : list3) {
			list4.add(classes.getCid());
		}

		UserExample example3 = new UserExample();
		com.woniu.tms.entity.UserExample.Criteria criteria3 = example3.createCriteria();
		criteria3.andClassidIn(list4);
		List<User> list5 = userMapper.selectByExample(example3);
		List<Integer> list6 = new ArrayList<>();
		for (User user : list5) {
			list6.add(user.getUid());
		}

		InterviewExample example = new InterviewExample();
		com.woniu.tms.entity.InterviewExample.Criteria criteria = example.createCriteria();
		criteria.andStudentidIn(list6);
		List<Interview> list2 = interviewMapper.selectByExample(example);
		System.out.println(list2.size());
		if (info != null && !info.trim().equals("")) {
			for (Interview interview : list2) {
				if (interview.getCompanyUser().getUname().contains(info)) {
					list.add(interview);
				} else if (interview.getStudentid().toString().contains(info)) {
					list.add(interview);
				}
			}

			return list;
		}
		return list2;
	}

	@Override
	public List<Interview> findLikeUname2(String info, User loginUser, String className) {
		List<Interview> list = new ArrayList<>();
		List<Integer> list4 = new ArrayList<>();
		ClassesExample example2 = new ClassesExample();
		com.woniu.tms.entity.ClassesExample.Criteria criteria2 = example2.createCriteria();
		className = className.trim();
		criteria2.andCnameEqualTo(className);
		List<Classes> list3 = classesMapper.selectByExample(example2);
		for (Classes classes : list3) {
			list4.add(classes.getCid());
		}

		UserExample example3 = new UserExample();
		com.woniu.tms.entity.UserExample.Criteria criteria3 = example3.createCriteria();
		criteria3.andClassidIn(list4);
		List<User> list5 = userMapper.selectByExample(example3);
		List<Integer> list6 = new ArrayList<>();
		for (User user : list5) {
			list6.add(user.getUid());
		}

		InterviewExample example = new InterviewExample();
		com.woniu.tms.entity.InterviewExample.Criteria criteria = example.createCriteria();
		criteria.andStudentidIn(list6);
		List<Interview> list2 = interviewMapper.selectByExample(example);

		if (info != null && !info.trim().equals("")) {
			for (Interview interview : list2) {
				if (interview.getStudentUser().getUname().contains(info)) {
					list.add(interview);
				} else if (interview.getStudentid().toString().contains(info)) {
					list.add(interview);
				}
			}

			return list;
		}
		return list2;
	}

	@Override
	public List<Classes> finAllClasses() {
		List<Classes> list = classesMapper.selectCname();
		return list;
	}

	@Override
	public void saverecive(String review, Integer studentid) {
//		System.out.println(studentid);
//		InterviewExample example = new InterviewExample();
//		com.woniu.tms.entity.InterviewExample.Criteria criteria = example.createCriteria();
//		criteria.andStudentidEqualTo(studentid);
//		List<Interview> interview = interviewMapper.selectByExample(example);
//		Interview interview2 = interview.get(0);
//		interview2.setReview(recive);
		System.out.println("-----------------"+review);
		interviewMapper.updateByStudentID(review, studentid);

	}

	@Override
	public void savetips(String tips, Integer iid) {
		InterviewExample example = new InterviewExample();
		com.woniu.tms.entity.InterviewExample.Criteria criteria = example.createCriteria();
		criteria.andIidEqualTo(iid);
		List<Interview> list = interviewMapper.selectByExample(example);
		Interview interview = list.get(0);
		interview.setTips(tips);
		interviewMapper.updateByExample(interview, example);

	}

	@Override
	public void updateOne(Interview interview) {
		InterviewExample example = new InterviewExample();
		com.woniu.tms.entity.InterviewExample.Criteria criteria = example.createCriteria();
		criteria.andStudentidEqualTo(interview.getStudentid());
		List<Interview> list = interviewMapper.selectByExample(example);
		Interview interview2 = list.get(0);
		interview2.setComments(interview.getComments());
		interview2.setResult(interview.getResult());
		interviewMapper.updateByExample(interview2, example);

	}

	@Override
	public List<Interview> finByUnameLike3(String info, User user) {
		List<Interview> list2 = new ArrayList<>();
		InterviewExample example = new InterviewExample();
		com.woniu.tms.entity.InterviewExample.Criteria criteria = example.createCriteria();
		criteria.andCompanyidEqualTo(user.getUid());
		List<Interview> list = interviewMapper.selectByExample(example);
		if (info != null && !info.trim().equals("")) {
			for (Interview interview : list) {
				if (interview.getStudentUser().getUname().contains(info)) {
					list2.add(interview);
				} else if (interview.getStudentid().toString().contains(info)) {
					list2.add(interview);
				}
			}
			return list2;
		}
		return list;

	}

	@Override
	public List<Interview> finByUnameLike4(String info, User user) {
		CompanyinfoExample example = new CompanyinfoExample();
		List<Interview> list4 = new ArrayList<>();
		Criteria criteria = example.createCriteria();
		criteria.andEmpidEqualTo(user.getUid());
		List<Companyinfo> list = companyinfoMapper.selectByExample(example);
		List<Integer> list2=new ArrayList<>();
		for (Companyinfo companyinfo : list) {
			list2.add(companyinfo.getCompanyid());
		}
		InterviewExample example2= new InterviewExample();
		com.woniu.tms.entity.InterviewExample.Criteria criteria2 = example2.createCriteria();
		criteria2.andCompanyidIn(list2);
		List<Interview> list3 = interviewMapper.selectByExample(example2);
		for (Interview interview : list3) {
			System.out.println(interview.getStudentUser().getUname());
		}
		if (info != null && !info.trim().equals("")) {
			for (Interview interview : list3) {
				
				if (interview.getStudentUser().getUname().contains(info)||interview.getStudentid().toString().contains(info)) {
					list4.add(interview);
				} 
			}
			return list4;
		}
		return list3;
	}
}
